/**
 * Copyright (c) 2011 - 2012, Sina Inc. All rights reserved.
 * @fileoverview Sina  表单-radio和checkBox基础类
 * @author  bowen liu | liubowen082@163.com
 * @version 1.0 | 2012-2-21
 * @param
 * opt{
 * 	'el': '',       // 父容器标签
 * 	'isMust': '',   // 是否必填
 * 	'isChange':''   // 是否实时检测
 * }
 * @example
 * var a = new STK.module.form.formInputRc(opt);
 */
LJS.define('form.formInputRc', function(ljs){
    var formInputRc = function(opt){
        var that = this;
        if (!opt || !opt.el) 
            return false;
        // 父容器标签
        that.el = opt.el;
        // 是否是必选项,默认不是必选(供外部重写)
        that.isMust = opt.isMust || 0;
        // 是否实时判断合法性,默认不开启(供外部重写)
        that.isChange = opt.isChange || 0;
    };
    formInputRc.prototype = {
        init: function(){
            var that = this;
        },
        /**
         * 获得容器中的所有radio标签或者checkedbox标签
         * @param {String} what 'radio' || 'checkbox'
         * @return Array 标签数组
         */
        getRc: function(what){
            var that = this;
            return what === 'checkbox' ? $.sizzle('input[type=checkbox]', that.el) : $.sizzle('input[type=radio]', that.el);
        },
        /**
         * 控件单击事件
         * @param {Object} opt 配置参数
         * opt{
         * 	callback:function(spec){
         *		console.info(spec.el, spec.data);
         *  }
         * }
         */
        click: function(opt){
            var that = this;
            if (!opt || !opt.callback) 
                return false;
           //var _par = $.core.evt.delegatedEvent(that.el);
            // radio不允许重复点击一个
            var _oldRaido = '';
            $(that.el).on('click' , '[type=radio]', function(spec){
                if (_oldRaido) 
                    _oldRaido.setAttribute('action_type', 'radio');
                _oldRaido = this;
                opt.callback(this);
                this.setAttribute('action_type', 'cur');
            });
            $(that.el).on('click' , '[type=checkbox]' , function(){
                opt.callback(this);
            });
        },
        /**
         * 设置标签为选中项
         * @param {Object} value 标签上的value值
         */
        setChecked: function(value){
            var that = this;
            $.foreach(that.getRc('checkbox'), function(a){
                a.getAttribute('value') === value ? a.checked = 'checked' : '';
            });
            $.foreach(that.getRc(), function(a){
                a.getAttribute('value') === value ? a.checked = 'checked' : '';
            });
        },
        /**
         * 获得选择的radio标签或者checkbox标签
         * @param {Object} what 'radio' || 'checkbox'
         * @return Array 选择的标签数组
         */
        getCheckedRc: function(what){
            var that = this;
            var _checkedArr = [];
            if (what === 'checkbox') {
                $.foreach(that.getRc('checkbox'), function(a){
                    a.checked ? _checkedArr.push(a) : '';
                });
            } else {
                $.foreach(that.getRc(), function(a){
                    a.checked ? _checkedArr.push(a) : '';
                });
            }
            return _checkedArr;
        }
        /**
         * 以下都为空方法,供外部重写
         * check: function(){
           },
           showTip: function(){
           },
           showError: function(){
           },
           hide: function(){
           }
         */
    };
    return formInputRc;
});
